package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.Dict;
import com.atguigu.mapper.DictMapper;
import com.atguigu.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shf
 *
 * @author: cbc
 * time: 2022/6/8 20:35 周三
 * description:
 */
@Service(interfaceClass = DictService.class)
public class DictServiceImpl extends BaseServiceImpl<Dict> implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<Map<String, Object>> findZnodes(Long id) {

        List<Dict> dictList = dictMapper.findListByParentId(id);

        List<Map<String, Object>> zNodes = new ArrayList<>();

        for (Dict dict : dictList) {
            //查询是否是父亲
            Integer count = dictMapper.countIsParent(dict.getId());
            Map<String, Object> map = new HashMap<>();
            map.put("id", dict.getId());
            map.put("isParent", count > 0 ? true : false);
            map.put("name", dict.getName());
            zNodes.add(map);
        }
        return zNodes;
    }

    @Override
    public List<Dict> findListByParentId(Long parentId) {
        return dictMapper.findListByParentId(parentId);
    }

    @Override
    public List<Dict> findListByDictCode(String dictCode) {
        Dict dict = dictMapper.getByDictCode(dictCode);
        if (dict == null) {
            return null;
        }
        return dictMapper.findListByParentId(dict.getId());
    }

    @Override
    public String getNameById(Long houseTypeId) {
        return dictMapper.getNameById(houseTypeId);
    }

    @Override
    protected BaseMapper<Dict> getEntityMapper() {
        return dictMapper;
    }
}
